




/*
'===============================================================================
'  HNG.Portal.BL.TblTagManagerBase
'===============================================================================
*/

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Windows.Forms;
namespace HNG.Portal.BLL
{
	public class TblTagManagerBase
	{
	    
		#region ColumnNames
		
				public const String Field_Id="Id";
				
				public const String Field_TagName="TagName";
				
				public const String Field_Url="Url";
				
				public const String Field_TrangThai="TrangThai";
				
				public const String Field_TinTucId="TinTucId";
				
				public const String Field_SanPhamId="SanPhamId";
				
		#endregion	
		/// <summary>
		/// Purpose: Class constructor.
		/// </summary>
		public TblTagManagerBase()
		{
			// Nothing for now.
		}
		public DataTable Clone()
		{
			DataTable dt = new DataTable();
						
			
			dt.Columns.Add(Field_Id,typeof(int));
			
						
			
			dt.Columns.Add(Field_TagName,typeof(string));
			
						
			
			dt.Columns.Add(Field_Url,typeof(string));
			
						
			
			dt.Columns.Add(Field_TrangThai,typeof(bool));
			
						
			
			dt.Columns.Add(Field_TinTucId,typeof(int));
			
						
			
			dt.Columns.Add(Field_SanPhamId,typeof(int));
			
			            
            return dt;			
		}
			
		public TblTagEntity Convert_Entity(TblTagEntity _TblTagEntity,TblTagEntity _TblTagEntity_XML)
		{	
			
				
			_TblTagEntity.Id= _TblTagEntity_XML.Id;
				
			_TblTagEntity.TagName= _TblTagEntity_XML.TagName;
				
			_TblTagEntity.Url= _TblTagEntity_XML.Url;
				
			_TblTagEntity.TrangThai= _TblTagEntity_XML.TrangThai;
				
			_TblTagEntity.TinTucId= _TblTagEntity_XML.TinTucId;
				
			_TblTagEntity.SanPhamId= _TblTagEntity_XML.SanPhamId;
				
			
			return _TblTagEntity;
		}	
		
			
		public void InsertCollection(EntityCollection _Collection)
		{
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
            {
                adapter.SaveEntityCollection(_Collection);
            }
		}

		public TblTagEntity Insert(TblTagEntity TblTagEntity)
		{
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.SaveEntity(TblTagEntity, true);
			}
			return TblTagEntity;
		}

		
		public TblTagEntity Insert(int Id, string TagName, string Url, bool TrangThai, int TinTucId, int SanPhamId)
		{
			TblTagEntity _TblTagEntity = new TblTagEntity();
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				
				_TblTagEntity.Id = Id;
				_TblTagEntity.TagName = TagName;
				_TblTagEntity.Url = Url;
				_TblTagEntity.TrangThai = TrangThai;
				_TblTagEntity.TinTucId = TinTucId;
				_TblTagEntity.SanPhamId = SanPhamId;
				adapter.SaveEntity(_TblTagEntity, true);
			}
			return _TblTagEntity;
		}

		public TblTagEntity Insert(string TagName, string Url, bool TrangThai, int TinTucId, int SanPhamId)
		{
			TblTagEntity _TblTagEntity = new TblTagEntity();
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				
				_TblTagEntity.TagName = TagName;
				_TblTagEntity.Url = Url;
				_TblTagEntity.TrangThai = TrangThai;
				_TblTagEntity.TinTucId = TinTucId;
				_TblTagEntity.SanPhamId = SanPhamId;
				adapter.SaveEntity(_TblTagEntity, true);
			}
			return _TblTagEntity;
		}

		public bool Update(TblTagEntity _TblTagEntity)
		{
			bool toReturn = false;
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				RelationPredicateBucket filter = new RelationPredicateBucket();
				IPredicateExpression _PredicateExpression = new PredicateExpression();
				_PredicateExpression.Add(TblTagFields.Id == _TblTagEntity.Id);
					
				filter.PredicateExpression.Add(_PredicateExpression);

				adapter.UpdateEntitiesDirectly(_TblTagEntity, filter);
				toReturn = true;
			}
			return toReturn;
		}
		
		public bool Update(TblTagEntity _TblTagEntity, RelationPredicateBucket filter)
		{
			bool toReturn = false;
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.UpdateEntitiesDirectly(_TblTagEntity, filter);
				toReturn = true;
			}
			return toReturn;
		}

		public bool Update(int Id, string TagName, string Url, bool TrangThai, int TinTucId, int SanPhamId)
		{
			bool toReturn = false;
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				TblTagEntity _TblTagEntity = new TblTagEntity(Id);
				if (adapter.FetchEntity(_TblTagEntity))
				{
				
					_TblTagEntity.TagName = TagName;
					_TblTagEntity.Url = Url;
					_TblTagEntity.TrangThai = TrangThai;
					_TblTagEntity.TinTucId = TinTucId;
					_TblTagEntity.SanPhamId = SanPhamId;
					adapter.SaveEntity(_TblTagEntity, true);
					toReturn = true;
				}
			}
			return toReturn;
		}

		public bool Delete(int Id)
		{
			bool toReturn = false;
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				TblTagEntity _TblTagEntity = new TblTagEntity(Id);
				if (adapter.FetchEntity(_TblTagEntity))
				{
					adapter.DeleteEntity(_TblTagEntity);
					toReturn = true;
				}
			}
			return toReturn;
		}
		
		public void DeleteAll() 
		{
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.DeleteEntitiesDirectly("TblTagEntity", null);
			}
		}
		
		
		public int DeleteById(int Id)
		{
			int toReturn = 0;
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblTagFields.Id == Id);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				toReturn = adapter.DeleteEntitiesDirectly("TblTagEntity", filter);
			}
			return toReturn;
		}
		
		public int DeleteByTagName(string TagName)
		{
			int toReturn = 0;
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblTagFields.TagName == TagName);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				toReturn = adapter.DeleteEntitiesDirectly("TblTagEntity", filter);
			}
			return toReturn;
		}
		
		public int DeleteByUrl(string Url)
		{
			int toReturn = 0;
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblTagFields.Url == Url);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				toReturn = adapter.DeleteEntitiesDirectly("TblTagEntity", filter);
			}
			return toReturn;
		}
		
		public int DeleteByTrangThai(bool TrangThai)
		{
			int toReturn = 0;
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblTagFields.TrangThai == TrangThai);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				toReturn = adapter.DeleteEntitiesDirectly("TblTagEntity", filter);
			}
			return toReturn;
		}
		
		public int DeleteByTinTucId(int TinTucId)
		{
			int toReturn = 0;
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblTagFields.TinTucId == TinTucId);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				toReturn = adapter.DeleteEntitiesDirectly("TblTagEntity", filter);
			}
			return toReturn;
		}
		
		public int DeleteBySanPhamId(int SanPhamId)
		{
			int toReturn = 0;
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblTagFields.SanPhamId == SanPhamId);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				toReturn = adapter.DeleteEntitiesDirectly("TblTagEntity", filter);
			}
			return toReturn;
		}
		

		
		public TblTagEntity SelectOne(int Id)
		{
			TblTagEntity toReturn = null;
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				TblTagEntity _TblTagEntity = new TblTagEntity(Id);
				if (adapter.FetchEntity(_TblTagEntity))
				{
					toReturn = _TblTagEntity;
				}
			}
			return toReturn;
		}

		// Return EntityCollection
		public EntityCollection SelectAll()
		{
			EntityCollection _TblTagCollection = new EntityCollection( new TblTagEntityFactory());
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchEntityCollection(_TblTagCollection, null, 0, null);
			}
			return _TblTagCollection;
		}
		
		// Return DataTable
		public DataTable SelectAllRDT()
		{
			DataTable toReturn = new DataTable();
			EntityCollection _TblTagCollection = new EntityCollection(new TblTagEntityFactory());
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchTypedList(_TblTagCollection.EntityFactoryToUse.CreateFields(), toReturn, null, true);
			}
			return toReturn;
		}

		
		// Return EntityCollection
		public EntityCollection SelectById(int Id)
		{
			EntityCollection _TblTagCollection = new EntityCollection(new TblTagEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();

			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblTagFields.Id == Id);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchEntityCollection(_TblTagCollection, filter, 0, null);
			}
			return _TblTagCollection;
		}
		
		// Return DataTable
		public DataTable SelectByIdRDT(int Id)
		{
			DataTable toReturn = new DataTable();
			EntityCollection _TblTagCollection = new EntityCollection(new TblTagEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblTagFields.Id == Id);
			filter.PredicateExpression.Add(_PredicateExpression);

			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchTypedList(_TblTagCollection.EntityFactoryToUse.CreateFields(), toReturn, filter, true);
			}
			return toReturn;
		}
		
		// Return EntityCollection
		public EntityCollection SelectByTagName(string TagName)
		{
			EntityCollection _TblTagCollection = new EntityCollection(new TblTagEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();

			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblTagFields.TagName == TagName);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchEntityCollection(_TblTagCollection, filter, 0, null);
			}
			return _TblTagCollection;
		}
		
		// Return DataTable
		public DataTable SelectByTagNameRDT(string TagName)
		{
			DataTable toReturn = new DataTable();
			EntityCollection _TblTagCollection = new EntityCollection(new TblTagEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblTagFields.TagName == TagName);
			filter.PredicateExpression.Add(_PredicateExpression);

			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchTypedList(_TblTagCollection.EntityFactoryToUse.CreateFields(), toReturn, filter, true);
			}
			return toReturn;
		}
		
		// Return EntityCollection
		public EntityCollection SelectByUrl(string Url)
		{
			EntityCollection _TblTagCollection = new EntityCollection(new TblTagEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();

			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblTagFields.Url == Url);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchEntityCollection(_TblTagCollection, filter, 0, null);
			}
			return _TblTagCollection;
		}
		
		// Return DataTable
		public DataTable SelectByUrlRDT(string Url)
		{
			DataTable toReturn = new DataTable();
			EntityCollection _TblTagCollection = new EntityCollection(new TblTagEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblTagFields.Url == Url);
			filter.PredicateExpression.Add(_PredicateExpression);

			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchTypedList(_TblTagCollection.EntityFactoryToUse.CreateFields(), toReturn, filter, true);
			}
			return toReturn;
		}
		
		// Return EntityCollection
		public EntityCollection SelectByTrangThai(bool TrangThai)
		{
			EntityCollection _TblTagCollection = new EntityCollection(new TblTagEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();

			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblTagFields.TrangThai == TrangThai);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchEntityCollection(_TblTagCollection, filter, 0, null);
			}
			return _TblTagCollection;
		}
		
		// Return DataTable
		public DataTable SelectByTrangThaiRDT(bool TrangThai)
		{
			DataTable toReturn = new DataTable();
			EntityCollection _TblTagCollection = new EntityCollection(new TblTagEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblTagFields.TrangThai == TrangThai);
			filter.PredicateExpression.Add(_PredicateExpression);

			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchTypedList(_TblTagCollection.EntityFactoryToUse.CreateFields(), toReturn, filter, true);
			}
			return toReturn;
		}
		
		// Return EntityCollection
		public EntityCollection SelectByTinTucId(int TinTucId)
		{
			EntityCollection _TblTagCollection = new EntityCollection(new TblTagEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();

			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblTagFields.TinTucId == TinTucId);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchEntityCollection(_TblTagCollection, filter, 0, null);
			}
			return _TblTagCollection;
		}
		
		// Return DataTable
		public DataTable SelectByTinTucIdRDT(int TinTucId)
		{
			DataTable toReturn = new DataTable();
			EntityCollection _TblTagCollection = new EntityCollection(new TblTagEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblTagFields.TinTucId == TinTucId);
			filter.PredicateExpression.Add(_PredicateExpression);

			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchTypedList(_TblTagCollection.EntityFactoryToUse.CreateFields(), toReturn, filter, true);
			}
			return toReturn;
		}
		
		// Return EntityCollection
		public EntityCollection SelectBySanPhamId(int SanPhamId)
		{
			EntityCollection _TblTagCollection = new EntityCollection(new TblTagEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();

			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblTagFields.SanPhamId == SanPhamId);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchEntityCollection(_TblTagCollection, filter, 0, null);
			}
			return _TblTagCollection;
		}
		
		// Return DataTable
		public DataTable SelectBySanPhamIdRDT(int SanPhamId)
		{
			DataTable toReturn = new DataTable();
			EntityCollection _TblTagCollection = new EntityCollection(new TblTagEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblTagFields.SanPhamId == SanPhamId);
			filter.PredicateExpression.Add(_PredicateExpression);

			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchTypedList(_TblTagCollection.EntityFactoryToUse.CreateFields(), toReturn, filter, true);
			}
			return toReturn;
		}
		
		

	}
}
